#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
using namespace std;
const int N=1e4+10;
typedef pair<string,string>PSS;
map<string,int>f;
map<string,int>vis;
vector<PSS>arr;
int n,m;
struct people{
	string name;
	string id;
	int flag;
	int hh,mm;
	bool operator <(const people &p)const{
		if(hh==p.hh)
		return mm<p.mm;
		return hh<p.hh;
	}
}; 
int t,s;
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>t>>s;
		people p[N];
		for(int j=1;j<=t;j++)
		{
			cin>>p[j].name>>p[j].id>>p[j].flag;
			scanf("%d:%d",&p[j].hh,&p[j].mm);
			if(p[j].flag==1&&!vis[p[j].name])
			{
				vis[p[j].name]++;
				arr.push_back({p[j].name,p[j].id});
			}
		}
		sort(p+1,p+n+1);
		for(int j=1;j<=n;j++)
		{
			if(!s)
			break;
            if(p[j].id.size()!=18)
                continue;
			if(!f[p[j].id])
			{
				s--;
				cout<<p[j].name<<' '<<p[j].id<<endl;
                f[p[j].id]=i;
			}
			else if(i-f[p[j].id]>m)
			{
				s--;
				cout<<p[j].name<<' '<<p[j].id<<endl;
			}
		}
	}
	for(int i=0;i<arr.size();i++)
		cout<<arr[i].first<<' '<<arr[i].second<<endl;
	return 0;
} 
